package com.artisan.service.wx.impl;

import com.artisan.dao.*;
import com.artisan.medium.wx.service.WxBusinessService;
import com.artisan.model.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by yang on 2017/6/18.
 */
@Service("wxBusinessService")
public class WxBusinessServiceImpl implements WxBusinessService {
    @Autowired
    private BusinessTypeMapper businessTypeMapper;

    @Autowired
    private BusinessMapper businessMapper;


    @Autowired
    private AdvertMapper advertMapper;

    @Autowired
    private CustomerOrderinfoMapper customerOrderinfoMapper;
    @Autowired
    private CustomerPayinfoMapper customerPayinfoMapper;

    @Override
    public Map<String, Object> qService() {
        Map<String,Object>map=new HashMap<String,Object>();
        BusinessTypeExample businessExample = new BusinessTypeExample();
        BusinessTypeExample.Criteria bCriteria = businessExample.createCriteria();
        bCriteria.andPidNotEqualTo(0);
        List<BusinessType> businesss = businessTypeMapper.selectByExample(businessExample);
        map.put("services",businesss);
        AdvertExample advertExample = new AdvertExample();
        AdvertExample.Criteria adCriteria = advertExample.createCriteria();
        adCriteria.andStateEqualTo(0);
        List<Advert> ads = advertMapper.selectByExample(advertExample);
        map.put("ads",ads);
        return map;
    }

    @Override
    public List<Business> qServices(int bid) {
        BusinessExample businessExample = new BusinessExample();
        BusinessExample.Criteria bCriteria = businessExample.createCriteria();
        bCriteria.andPidEqualTo(bid);
        return businessMapper.selectByExample(businessExample);
    }

    @Override
    public Business qService(int bid) {
        return businessMapper.selectByPrimaryKey(bid);
    }

    @Override
    public void addOrder(CustomerOrderinfo customerOrderinfo) {
        customerOrderinfo.setCrtTime(new Date());
        customerOrderinfo.setState(1);
        customerOrderinfo.setUpdateTime(new Date());
        customerOrderinfoMapper.insert(customerOrderinfo);
    }

    @Override
    public void savePayInfo(CustomerPayinfo payinfo) {
        customerPayinfoMapper.insert(payinfo);
    }

    @Override
    public CustomerPayinfo getPayInfo(String trade_no) {
        CustomerPayinfoExample example = new CustomerPayinfoExample();
        CustomerPayinfoExample.Criteria bCriteria = example.createCriteria();
        bCriteria.andTradeNoEqualTo(trade_no);
        List<CustomerPayinfo> customerPayinfoList= customerPayinfoMapper.selectByExample(example);
        if(customerPayinfoList.size()!=0){
            return  customerPayinfoList.get(0);
        }else{
            return  null;
        }
    }

    @Override
    public int updateOrderState(String order_no) {
        CustomerOrderinfoExample example=new CustomerOrderinfoExample();
        CustomerOrderinfoExample.Criteria criteria=example.createCriteria();
        criteria.andOrderNoEqualTo(order_no);
        List<CustomerOrderinfo>list=customerOrderinfoMapper.selectByExample(example);
        CustomerOrderinfo customerOrderinfo=list.get(0);
        customerOrderinfo.setState(2);
        customerOrderinfo.setUpdateTime(new Date());
        return customerOrderinfoMapper.updateByPrimaryKey(customerOrderinfo);
    }
}
